﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DAL
{
    public class HocPhiProvider
    {
        public static List<BienLai> LayDanhSachBienLai()
        {
            var lst = new List<BienLai>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.BienLais select u);

                foreach (var row in query)
                {
                    var bl = new BienLai();
                    bl.MaBienLai = row.MaBienLai;
                    bl.MaDangKy = row.MaDangKy;
                    bl.MaHV = row.MaHV;
                    bl.MaNV = row.MaNV;
                    bl.NgayThu = row.NgayThu;
                    bl.TienHP = row.TienHP;
                    lst.Add(bl);
                }
            }

            return lst;
        }

        public static List<BienLai> TimDanhSachHocPhi(object objA, SoSanh ss)
        {
            var lst = new List<BienLai>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.BienLais select u);

                foreach (var row in query)
                {
                    var bl = new BienLai();
                    bl.MaBienLai = row.MaBienLai;
                    bl.MaDangKy = row.MaDangKy;
                    bl.MaHV = row.MaHV;
                    bl.MaNV = row.MaNV;
                    bl.NgayThu = row.NgayThu;
                    bl.TienHP = row.TienHP;

                    if (ss(objA, bl) == 0)
                    {
                        lst.Add(bl);
                    }
                }
            }

            return lst;
        }

        public static BienLai TimHocPhi(object objA, SoSanh ss)
        {
            var bienlai = new BienLai() ;
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.BienLais select u);

                foreach (var row in query)
                {
                    var bl = new BienLai();
                    bl.MaBienLai = row.MaBienLai;
                    bl.MaDangKy = row.MaDangKy;
                    bl.MaHV = row.MaHV;
                    bl.MaNV = row.MaNV;
                    bl.NgayThu = row.NgayThu;
                    bl.TienHP = row.TienHP;

                    if (ss(objA, bl) == 0)
                    {
                        bienlai = bl;
                        break;
                    }
                }
            }

            return bienlai;
        }


        public static List<BienLai> TimDanhSachHocPhiTheoNgay(object a, object b, SoTrongKhoang ss)
        {
            var lst = new List<BienLai>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.BienLais select u);

                foreach (var row in query)
                {
                    var bl = new BienLai();
                    bl.MaBienLai = row.MaBienLai;
                    bl.MaDangKy = row.MaDangKy;
                    bl.MaHV = row.MaHV;
                    bl.MaNV = row.MaNV;
                    bl.NgayThu = row.NgayThu;
                    bl.TienHP = row.TienHP;

                    if (ss(a, b, bl))
                    {
                        lst.Add(bl);
                    }
                }
            }

            return lst;
        }

        public static int ThemHocPhi(BienLai hocphi)
        {
            int maBienLai = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.BienLais.Add(hocphi);
                    context.SaveChanges();
                    maBienLai = hocphi.MaBienLai;
                }
            }
            catch
            {

            }
            return maBienLai;
        }



        public static bool XoaHocPhi(int maBienLai)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.BienLais
                                 where u.MaBienLai.Equals(maBienLai)
                                 select u).First();

                    context.BienLais.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        public static bool CapNhatHocPhi(BienLai hocphi)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.BienLais
                                 where u.MaBienLai.Equals(hocphi.MaBienLai)
                                 select u).First();

                    query.MaDangKy = hocphi.MaDangKy;
                    query.MaHV = hocphi.MaHV;
                    query.MaNV = hocphi.MaNV;
                    query.NgayThu = hocphi.NgayThu;
                    query.TienHP = hocphi.TienHP;
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
